<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/2/10 17:18
 * Remark:
 */

namespace App\Func;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Consts\WordConsts\HdPdfConsts;
use Illuminate\Http\Exceptions\HttpResponseException;

class WordToPdfFuc
{
    public static function wordToPdf($url = "",$watermark = "",$pdffilename = ""){
        if(!$url){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前文件不支持预览",
                ResponseStatusConsts::UNAUTHORIZED_401_RESPONSE_STATUS
            ));
        }
        // 云市场分配的密钥Id
        $secretId = HdPdfConsts::Hd_PDF_SECRET_ID;
        //云市场分配的密钥Key
        $secretKey = HdPdfConsts::Hd_PDF_SECRET_KEY;
        $source = 'market';
        //签名
        $datetime = gmdate('D, d M Y H:i:s T');
        $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source);
        $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
        $auth = sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign);
        // 请求方法
        $method = 'GET';
        //请求头
        $headers = array(
            'X-Source' => $source,
            'X-Date' => $datetime,
            'Authorization' => $auth,
        );

        //查询参数
        $queryParams = array (
            'callbackurl' => '',
            'compress' => '0',
            'exceliscenter' => '0',
            'excelislandscape' => '0',
            'excelmargin' => '10',
            'excelnotshowgridlines' => '0',
            'excelsheetindex' => '0',
            'exceluseprintarea' => '0',
            'imageclean' => '0',
            'imagedeskew' => '0',
            'imageocr' => '0',
            'linearization' => '0',
            'ownerpassword' => '',
            'pdffilename' => $pdffilename,
            'pdfrestriction' => '000',
            'type' => 'docx',
            'url' => $url,
            'userpassword' => '',
            'watermark' => $watermark,
            'wordshowmarkup' => '0',
        );
        // body参数（POST方法下）
        $bodyParams = array (

        );
        // url参数拼接
        $url = 'https://service-gsefnc5p-1256652084.sh.apigw.tencentcs.com/release/v1/convert';
        if (count($queryParams) > 0) {
            $url .= '?' . http_build_query($queryParams);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function ($v, $k) {
            return $k . ': ' . $v;
        }, array_values($headers), array_keys($headers)));
        if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($bodyParams));
        }
        $data = curl_exec($ch);
        if (curl_errno($ch)) {
            echo "Error: " . curl_error($ch);
        } else {
            return json_decode($data,true)['result']['token'];
        }
    }

    public static function showHtml($token){
        // 云市场分配的密钥Id
        $secretId = 'AKID6w1Jq56U1lfvargaC44OSJgr2YF6I3ws05i0';
        // 云市场分配的密钥Key
        $secretKey = 'h987Pc2leJifa9Jc0mogiP4Tw3j5bl3w6xfeEcie';
        $source = 'market';

        // 签名
        $datetime = gmdate('D, d M Y H:i:s T');
        $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source);
        $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
        $auth = sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign);

        // 请求方法
        $method = 'GET';
        // 请求头
        $headers = array(
            'X-Source' => $source,
            'X-Date' => $datetime,
            'Authorization' => $auth,

        );
        // 查询参数
        $queryParams = array (
            'token' => $token,
        );
        // body参数（POST方法下）
        $bodyParams = array (

        );
// url参数拼接
        $url = 'https://service-gsefnc5p-1256652084.sh.apigw.tencentcs.com/release/v1/query';
        if (count($queryParams) > 0) {
            $url .= '?' . http_build_query($queryParams);
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function ($v, $k) {
            return $k . ': ' . $v;
        }, array_values($headers), array_keys($headers)));
        if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($bodyParams));
        }

        $data = curl_exec($ch);
        if (curl_errno($ch)) {
            echo "Error: " . curl_error($ch);
        } else {
            return json_decode($data,true);
        }
        curl_close($ch);
    }


}